﻿using System.Linq;
using Aluetjen.EmailService.Domain.Infrastructure;
using Aluetjen.EmailService.Domain.Models.Message;
using Aluetjen.EmailService.Domain.Models.Pipeline;

namespace Aluetjen.EmailService.Domain.Services
{
    public class RedirectionService : IPipelineService
    {
        public decimal Priority
        {
            get { return 20; }
        }

        private readonly IRepository<Redirection> _redirectionRepository;

        public RedirectionService(IRepository<Redirection> redirectionRepository)
        {
            _redirectionRepository = redirectionRepository;
        }

        public void ProcessMessage(MailMessage message)
        {
            var redirectionsOrderedByPriority = _redirectionRepository.All().OrderBy(p => p.Priority);

            foreach(var redirection in redirectionsOrderedByPriority)
            {
                redirection.TransformMessage(message);
            }
        }
    }
}